<?

class Delete extends Button {

    function render($sel) {
        if ($sel->isFileSelected()) {
            return $this->renderDialogButton('delete','Delete','Delete file');
        } elseif (($sel->isDirSelected()) && ($sel->object != '')) {
            $dir = $sel->getSelected();
            if (($dir->getFilesCount() == 0) && ($dir->getDirectoriesCount() == 0)) {
                return $this->renderDialogButton('delete','Delete','Delete directory');
            } else {
                return $this->renderDisabledButton('delete','Delete');
            }
        } else {
            return $this->renderDisabledButton('delete','Delete');
        }
    }

    function renderDialog($sel) {
        if ($sel->isFileSelected()) {
            $file = $sel->getSelected();
            return '<form method="POST" action="'.Config::getLink($_GET['path'],$_GET['object'],'&action=delete').'"><b>&nbsp;Warning!</b> File <b>'.basename($file->name).'</b> will be deleted. Are you sure? <input type="submit" value="Delete"></form>';
        } elseif (($sel->isDirSelected()) && ($sel->object != '')) {
            $file = $sel->getSelected();
            return '<form method="POST" action="'.Config::getLink($_GET['path'],$_GET['object'],'&action=delete').'"><b>&nbsp;Warning!</b> Directory <b>'.basename($file->name).'</b> will be deleted. Are you sure? <input type="submit" value="Delete"></form>';
        } else {
            return '';
        }
    }

    function action($sel) {
        if ($sel->isFileSelected()) {
            $file = $sel->getSelected();
            if (file_exists($file->name)) {
                @$file->delete();
            }
            header("Location: ".Config::getMsgLink($sel->getCurrentDirNameRel(),'',MESSAGE_INFO,'File <b>'.basename($file->name).'</b> was deleted successfully'));
            exit;
        } elseif (($sel->isDirSelected()) && ($sel->object != '')) {
            $dir = $sel->getSelected();
            if (($dir->getFilesCount() == 0) && ($dir->getDirectoriesCount() == 0)) {
                @$dir->delete();
            }
            header("Location: ".Config::getLink($sel->getParentDirNameRel()));
            exit;
        }
    }

}

?>